//
//  NewCarCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/28.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "NewCarCollectionViewCell.h"
#import "NewCarTableViewCell.h"
#import "NewCarModel.h"
#import "DetailViewController.h"


@interface NewCarCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;

// 在头视图创建一个View
@property (nonatomic, retain)UIView *headView;


// 头视图的数组
@property (nonatomic, retain)NSMutableArray *headArr;

@end



@implementation NewCarCollectionViewCell
-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    [_headView release];
    [_headArr release];
    [_navi release];
    [super dealloc];

}

//系统初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self createHeadView];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self dataHandle];
}

// 创建一个tableView
- (void)createTableView
{
    self.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 49 - 40) style:UITableViewStyleGrouped];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.contentView addSubview:self.tableV];
    
    [self.tableV registerClass:[NewCarTableViewCell class] forCellReuseIdentifier:@"NewCarTableViewCell"];
    
    
    
    [_tableV release];
}


// 数据解析
- (void)dataHandle
{
    [NetworkHandle GetDataWithURL:@"http://mrobot.pcauto.com.cn/v2/cms/channels/2?pageNo=1&pageSize=20&v=4.0.0" dic:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [NewCarModel transformWithArray:arr];
        
        [_tableV reloadData];
    } filed:^(NSError *error) {
        
    }];
}




#pragma mark - 建立头视图,(新车日历)
- (void)createHeadView
{
    // 创建头视图 View
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, BOUNDS_WIDTH - 20, 130)];
    self.headView.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    self.tableV.tableHeaderView = self.headView;
    
    // 上面标签
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 30)];
    titleLab.text = @"新车日历";
    titleLab.textColor = [UIColor lightGrayColor];
    [self.headView addSubview:titleLab];
    [titleLab release];
    
    // 一个灰色的View / 上面一个titleLab /头视图分为两个按钮 / 四个白色label,
    
    // 头部左边新车按钮及图片
    UIButton *btnLeft = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btnLeft.frame = CGRectMake(10, 60, (BOUNDS_WIDTH - 20 - 15) / 2, 60);
    btnLeft.backgroundColor = [UIColor whiteColor];
    [self.headView addSubview:btnLeft];
    
    UIImageView *imgLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btnLeft.frame.size.width / 2.0, btnLeft.frame.size.height)];
    [btnLeft addSubview:imgLeft];
    [imgLeft release];
    
    // 头部左边新车按钮(新车状态)
    UILabel *lableLeft = [[UILabel alloc] initWithFrame:CGRectMake(btnLeft.frame.size.width / 2.0 + 5, 0, btnLeft.frame.size.width / 2.0, 30)];
    lableLeft.font = [UIFont systemFontOfSize:13];
    lableLeft.textColor = [UIColor lightGrayColor];
    [btnLeft addSubview:lableLeft];
    [lableLeft release];
    
    // 头部左边新车按钮(新车型号)
    UILabel *lableLeftTwo = [[UILabel alloc] initWithFrame:CGRectMake(btnLeft.frame.size.width / 2.0 + 5, 30, btnLeft.frame.size.width / 2.0, 30)];
    lableLeftTwo.font = [UIFont systemFontOfSize:14];
    [btnLeft addSubview:lableLeftTwo];
    [lableLeftTwo release];
    
    // 头部右边新车按钮及图片
    UIButton *btnRight = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btnRight.frame = CGRectMake(25 + (BOUNDS_WIDTH - 20 - 15) / 2, 60, (BOUNDS_WIDTH - 20 - 15) / 2, 60);
    btnRight.backgroundColor = [UIColor whiteColor];
    [self.headView addSubview:btnRight];
    UIImageView *imgRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btnRight.frame.size.width / 2.0, btnRight.frame.size.height)];
    [btnRight addSubview:imgRight];
    [imgRight release];
    
    // 头部右边新车按钮(新车状态)
    UILabel *lableRight = [[UILabel alloc] initWithFrame:CGRectMake(btnRight.frame.size.width / 2.0 + 5, 0, btnRight.frame.size.width / 2.0, 30)];
    lableRight.font = [UIFont systemFontOfSize:13];
    lableRight.textColor = [UIColor lightGrayColor];
    [btnRight addSubview:lableRight];
    [lableRight release];
    
    // 头部右边新车按钮(新车型号)
    UILabel *lableRightTwo = [[UILabel alloc] initWithFrame:CGRectMake(btnRight.frame.size.width / 2.0 + 5, 30, btnRight.frame.size.width / 2.0, 30)];
    lableRightTwo.font = [UIFont systemFontOfSize:14];
    [btnRight addSubview:lableRightTwo];
    [lableRightTwo release];
    
    
    
    
    
    // 因为不想写属性 在这里写数据解析
    NSString *str = @"http://mrobot.pcauto.com.cn/v3/price/upcomingSerials";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        self.headArr = responseObject[@"data"];
        
        //取左边的一系列
        NSString *headImageLeftStr = self.headArr[0][@"image"];
        [imgLeft sd_setImageWithURL:[NSURL URLWithString:headImageLeftStr]];
        
        lableLeft.text = self.headArr[0][@"flag"];
        lableLeftTwo.text = self.headArr[0][@"serialGroupName"];
        
        
        // 取右边一系列
        NSString *headImageRightStr = self.headArr[1][@"image"];
        [imgRight sd_setImageWithURL:[NSURL URLWithString:headImageRightStr]];
        lableRight.text = self.headArr[1][@"flag"];
        lableRightTwo.text = self.headArr[1][@"serialGroupName"];
        
        [_tableV reloadData];
    } filed:^(NSError *error) {
        
    }];

    
    
}

#pragma mark - 点击tableview 进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    NewCarModel *newModel = self.dataArr[indexPath.row];
    
    detailVC.url = newModel.url;
    
    [self.navi pushViewController:detailVC animated:YES];
    
    
}


// tableview必须写的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCarTableViewCell" forIndexPath:indexPath];
    NewCarModel *ncModel = self.dataArr[indexPath.row];
    
    // 提取图片
    [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:ncModel.image]];
    cell.titleLab.text = ncModel.title;
    cell.countLab.text = [NSString stringWithFormat:@"评论数%@",ncModel.count];
    cell.timeLab.text = ncModel.pubDate;
    
    
    return cell;
}

// 设置高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}



@end
